<?php

namespace app\api\controller;

use app\api\model\Student as Student1;
use think\Filesystem;
class Student
{

    public $studentData;

    public function __construct()
    {
        //判断token是否有效，通过token读取学生的所有信息
        $token=input('get.token');
        if (empty($token)||$token=='-1')output_error('token为空');
        $this->studentData = Student1::where('student_token','=',$token)->find();
        if (empty($this->studentData)) output_error('token无效，请重新登录');

    }

    //注册功能
    public function getData()
    {
        //输出学生的所有信息，去除“student_password”字段
        unset($this->studentData['student_password']);
        unset($this->studentData['student_token']);
        output_data($this->studentData);
    }

    public function modifyName()
    {
        //修改学生姓名，学生姓名可重复。
        $name = input('get.name');
    }

    public function modifyAvatar()
    {
        //修改学生头像，需要自行创建文件夹，文件系统。
        //学生头像文件唯一，更新时需要删除原有的文件。
        $file = request()->file('image');
        $name = Filesystem::disk('avatar')->putFile('',$file,'uniqid');
        if ($this->studentData['student_avatar'] !='default.png'){
            $filename=app()->getRootPath().'pubiic\\upload\\avatar\\'.$this->studentData['student_avatar'];
            unlink($filename);
        }
        Student1::where('student_id','=',$this->studentData['student_id'])
            ->update(array('student_avatar'=>$name));
        output_data($name);
    }

}
